<template>
  <node
    :title="config.name"
    :show-error="showError"
    :content="content"
    :error-info="errorInfo"
    @selected="selected"
    @delNode="delNode"
    @insertNode="insertNode"
    placeholder="请设置抄送人"
    header-bgc="#409eff"
    header-icon="ele-Promotion"
  />
</template>

<script>
import Node from './Node.vue'
import { computed, reactive, toRefs } from 'vue'
export default {
    name: 'CcNode',
    components: { Node },
    props: {
        config: {
            type: Object,
            default: () => {
                return {}
            },
        },
    },
    emits: ['selected', 'delNode', 'insertNode'],
    setup(props, context) {
        const { config } = toRefs(props)
        const state = reactive({
            showError: false,
            errorInfo: '',
        })
        const selected = () => {
            context.emit('selected')
        }
        const delNode = () => {
            context.emit('delNode')
        }
        const insertNode = (type) => {
            context.emit('insertNode', type)
        }
        const content = computed(() => {
            if (config.value.props.shouldAdd) {
                return '由发起人指定'
            } else if (config.value.props.assignedUser.length > 0) {
                let texts = []
                config.value.props.assignedUser.forEach((org) => texts.push(org.name))
                return String(texts).replaceAll(',', '、')
            } else {
                return null
            }
        })
        //校验数据配置的合法性
        const validate = (err) => {
            state.showError = false
            if (config.value.props.shouldAdd) {
                state.showError = false
            } else if (config.value.props.assignedUser.length === 0) {
                state.showError = true
                this.errorInfo = '请选择需要抄送的人员'
            }
            if (state.showError) {
                err.push(`抄送节点 ${config.value.name} 未设置抄送人`)
            }
            return !state.showError
        }
        return {
            ...toRefs(state),
            selected,
            delNode,
            insertNode,
            content,
            validate
        }
    }
}
</script>

<style>

</style>